/**
 * 
 */
package br.org.group.android.gui;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import br.org.group.android.app.ConfigurationVO;
import br.org.group.android.app.R;
import br.org.group.android.dao.FortunesDbAdapter;

/**
 * @author julio
 * Activity responsável por configurar o comportamento da aplicação ao compartilhar uma frase.
 * Date:25/06/2011 
 */

public class ConfigureActivity extends Activity {
	private FortunesDbAdapter fdba;
	private Button salve;
	private RadioButton rbShareLink, rbShareMsg, rbAlwaysAsk, rbUseDefault, rbThemeDefault, rbThemeTeste;

	private OnClickListener salveListener = new OnClickListener() {

		public void onClick(View arg0) {
			fdba = new FortunesDbAdapter(ConfigureActivity.this);
			ConfigurationVO c = null;
			if (rbAlwaysAsk.isChecked()) {
				c = new ConfigurationVO(ConfigurationVO.CONFIG_SHARE,
						ConfigurationVO.SHARE_ALWAYS_ASK);

			}
			if (rbUseDefault.isChecked()) {
				if (rbShareLink.isChecked()) {
					c = new ConfigurationVO(ConfigurationVO.CONFIG_SHARE,
							ConfigurationVO.SHARE_LINK);
				} else if (rbShareMsg.isChecked()) {
					c = new ConfigurationVO(ConfigurationVO.CONFIG_SHARE, ConfigurationVO.SHARE_MSG);
				}

			}
			if (c != null) {
				fdba.atualizarConfiguracao(c);
			}
			
			c = null;
			if (rbThemeDefault.isChecked()) {
				c = new ConfigurationVO(ConfigurationVO.CONFIG_ICONS, ConfigurationVO.ICONS_CLASSIC);
			} else if (rbThemeTeste.isChecked()) {
				c = new ConfigurationVO(ConfigurationVO.CONFIG_ICONS, ConfigurationVO.ICONS_NEW);
			}
			if (c != null) {
				fdba.atualizarConfiguracao(c);
			}

			ConfigureActivity.this.finish();

		}
	};

	private OnCheckedChangeListener rbUseDefaultListener = new OnCheckedChangeListener() {

		public void onCheckedChanged(CompoundButton arg0, boolean arg1) {

			rbShareLink.setEnabled(arg1);
			rbShareMsg.setEnabled(arg1);

		}
	};
	
	private OnCheckedChangeListener rbAlwaysAskListener = new OnCheckedChangeListener() {

		public void onCheckedChanged(CompoundButton arg0, boolean arg1) {

			if (arg0.equals(rbAlwaysAsk)) {
				arg1 = !arg1;
			}
			
			rbShareLink.setEnabled(arg1);
			rbShareMsg.setEnabled(arg1);			
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.configure_activity);

		rbAlwaysAsk = (RadioButton) findViewById(R.id.rb_always_ask);
		rbShareLink = (RadioButton) findViewById(R.id.rb_share_link);
		rbShareMsg = (RadioButton) findViewById(R.id.rb_share_msg);
		rbUseDefault = (RadioButton) findViewById(R.id.rb_use_default);
		rbThemeDefault = (RadioButton) findViewById(R.id.rb_theme_default);
		rbThemeTeste = (RadioButton) findViewById(R.id.rb_theme_teste);

		rbUseDefault.setOnCheckedChangeListener(rbUseDefaultListener);
		rbAlwaysAsk.setOnCheckedChangeListener(rbAlwaysAskListener);

		boolean share = false;

		fdba = new FortunesDbAdapter(ConfigureActivity.this);
		ConfigurationVO c = fdba.getConfiguration(ConfigurationVO.CONFIG_SHARE);

		switch (c.getValue()) {
		case ConfigurationVO.SHARE_ALWAYS_ASK:
			rbAlwaysAsk.setChecked(true);
			share = false;
			break;

		case ConfigurationVO.SHARE_LINK:
			rbUseDefault.setChecked(true);
			rbShareLink.setChecked(true);
			share = true;

			break;

		case ConfigurationVO.SHARE_MSG:
			rbUseDefault.setChecked(true);
			rbShareMsg.setChecked(true);
			share = true;

			break;
		}
		rbShareLink.setEnabled(share);
		rbShareMsg.setEnabled(share);
		
		c = fdba.getConfiguration(ConfigurationVO.CONFIG_ICONS);
		switch (c.getValue()) {
		case ConfigurationVO.ICONS_CLASSIC:
			rbThemeDefault.setChecked(true);
			rbThemeTeste.setChecked(false);
			break;
		case ConfigurationVO.ICONS_NEW:
			rbThemeDefault.setChecked(false);
			rbThemeTeste.setChecked(true);
			break;
		}
		
		salve = (Button) findViewById(R.id.ibtn_save);
		salve.setOnClickListener(salveListener);
	
	
	
	
	}

	
}
